package cn.edu.zut.sunday.misc.controller;


import cn.edu.zut.sunday.base.controller.GenericController;
import cn.edu.zut.sunday.misc.domain.InformationGroup;
import cn.edu.zut.sunday.misc.domain.InformationSecurity;
import cn.edu.zut.sunday.misc.service.InformationGroupService;
import cn.edu.zut.sunday.misc.service.InformationService;
import cn.edu.zut.sunday.misc.vo.InformationQuery;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

@Api("HXC-信息安全数据接口")
@Slf4j
@RestController
@RequestMapping("infoSecurity")
@CrossOrigin
public class InfoSecurityController extends
  GenericController<InformationSecurity, Integer, InformationService> {
    @Autowired
    private InformationGroupService informationGroupService;
    @Autowired
    public void setInformationService(InformationService informationService) {
        this.service = informationService;
    }

    @Override
    protected InformationSecurity addModelCheck(InformationSecurity model,
                                                javax.servlet.http.HttpServletRequest request) {
        return null;
    }

    @Override
    protected Pageable createPageable(int page, int size) {
        Sort sort = Sort.by(Sort.Direction.DESC, "published");
        return PageRequest.of(page, size, sort);
    }

    @Override
    protected Specification<InformationSecurity> createSpecification(String requestBodyStr) {
        log.info("requestBodyStr: {}", requestBodyStr);
        final InformationQuery informationQuery = new Gson().fromJson(requestBodyStr,
                InformationQuery.class);
        log.info("InformationQuery: {}", informationQuery);

        if (informationQuery != null) {
            return getInformationSecuritySpecification(informationQuery);
        }
        return null;
    }

    //分页条件查询
    private Specification<InformationSecurity> getInformationSecuritySpecification(
            InformationQuery informationQuery) {
        String name = informationQuery.getName();
        String severity = informationQuery.getSeverity();
        String vulnType = informationQuery.getVulnType();
        Specification<InformationSecurity> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();

            if (!StringUtils.isEmpty(name)) {
                Predicate p1 = criteriaBuilder.like(root.get("name"), sqlLike(name));
                list.add(p1);
            }

            if (!StringUtils.isEmpty(severity)) {
                // 此处为查询serverName中含有key的数据
                Predicate p2 = criteriaBuilder.like(root.get("severity"), sqlLike(severity));
                list.add(p2);
            }

            if (!StringUtils.isEmpty(vulnType)) {
                // 此处为查询serverName中含有key的数据
                Predicate p2 = criteriaBuilder.like(root.get("vulnType"), sqlLike(vulnType));
                list.add(p2);
            }
          /*  //起始日期
            if (!StringUtils.isEmpty(published)) {
                Predicate p3 = criteriaBuilder.greaterThanOrEqualTo(root.get("published"), published);
                list.add(p3);
            }
           //结束日期
            if (!StringUtils.isEmpty(modified)) {
                Predicate p4 = criteriaBuilder.lessThanOrEqualTo(root.get("modified"), modified);
                list.add(p4);
            }*/

            return criteriaBuilder.and(list.toArray(new Predicate[0]));
        };

        return specification;
    }

    //增加方法
    @Override
    public InformationSecurity add(@RequestBody InformationSecurity informationSecurity,
                                   javax.servlet.http.HttpServletRequest request) {
        if (informationSecurity == null) {
            return null;
        }
        informationSecurity = setCommonAttr(informationSecurity, true);
        informationSecurity = this.service.save(informationSecurity);
        return informationSecurity;
    }

    //修改方法
    @Override
    public InformationSecurity update(@PathVariable Integer id,
                                      @RequestBody InformationSecurity informationSecurity, HttpServletRequest request) {
        informationSecurity.setId(id);
        informationSecurity = setCommonAttr(informationSecurity, false);
        informationSecurity = this.service.save(informationSecurity);
        return informationSecurity;
    }

    //删除方法
    @Override
    public void delete(@PathVariable Integer id) {

        this.service.delete(id);
    }

    //根据id查找方法
    @Override
    public InformationSecurity findById(@PathVariable Integer id) {

        return this.service.findById(id);
    }


    //判定是否存在类型
    @ApiOperation(value = "判定是否存在")
    @PostMapping(value = "/equal", produces = "application/json", consumes = "application/json")
    @ResponseBody
    public InformationSecurity equal(@RequestBody InformationSecurity informationSecurity) {
        Specification<InformationGroup> specification = (root, query, builder) ->
        query.where(
                builder.equal(root.get("vulnType"),informationSecurity.getVulnType())
        ).getRestriction();
        Pageable pageable = PageRequest.of(0,20);
       Page<InformationGroup> list = informationGroupService.findAll( specification,pageable);
       if (list.isEmpty()){
          InformationGroup informationGroup = new InformationGroup();
          informationGroup.setVulnType(informationSecurity.getVulnType());
          informationGroup.setPublished(informationSecurity.getPublished());
          informationGroup.setModified(informationSecurity.getModified());
          informationGroup.setCveId(informationSecurity.getCveId());
           informationGroupService.save(informationGroup);
     return  null;
        }else {

         return  informationSecurity;
 }
    }
}
